import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutterapp/config.dart';
import 'package:flutterapp/models/room_detail_data.dart';
import 'package:flutterapp/page/home/common_title/index.dart';
import 'package:flutterapp/page/home/info/index.dart';
import 'package:flutterapp/page/utils/dio_http.dart';
import 'package:flutterapp/page/widgets/common_detail_main.dart';
import 'package:flutterapp/page/widgets/common_swipper.dart';
import 'package:flutterapp/page/widgets/common_tag.dart';
import 'package:flutterapp/page/widgets/room_appliance.dart';
import 'package:share/share.dart';

import 'index_detail.dart';

class RoomDetailPage extends StatefulWidget {
  final String roomId;

  const RoomDetailPage({Key key, this.roomId}) : super(key: key);

  @override
  _RoomDetailPageState createState() => _RoomDetailPageState();
}

class _RoomDetailPageState extends State<RoomDetailPage> {
  RoomDetailData data;

  bool isLike = false;
  bool showAliText = false;

  _getDetail() async {
    try {
      print('${widget.roomId}');
      print('房屋详情进来了.....');
      var url = '/api/room/detail/${widget.roomId}';
      //发送请求
      var resMap = await DioHttp.of(context).get(url);
      //print(resMap);
      var resMapList = json.decode(resMap.toString())['body'];
      //这里必须要进行格式转换,不然会报错获取不到 ----这里是对象的数据转换
      var resDetailData = RoomDetailData.fromJson(resMapList);

      //这里是集合的转换
      //   data['area'].map((item) => GeneralType.fromJson(item)).toList());

      //对图片路径进行处理
      // resDetailData.houseImgs =
      //     resDetailData.houseImgs.map((e) => Config.QN + e).toList();
      //print(resDetailData.houseImgs);
      setState(() {
        data = resDetailData;
      });
    } catch (e) {
      print('房屋异常');
    }
  }

  initState() {
    super.initState();
    _getDetail();
    //这里是 拿的假数据
    // setState(() {
    //   data = defaualtData;
    // });
  }

  @override
  Widget build(BuildContext context) {
    //如果数据为空,那么返回空
    if (null == data) return Container();

    bool showTextTool = data.subTitle.length > 100;

    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(data.title),
        actions: [
          IconButton(
              icon: Icon(Icons.share),
              onPressed: () {
                //分享地址
                Share.share('http://www.baidu.com');
              })
        ],
      ),
      body: Stack(
        children: [
          ListView(
            children: [
              CommonSwipper(
                images: data.houseImgs,
              ),
              CommonTitle(data.title),
              Container(
                padding: EdgeInsets.only(left: 10.0),
                child: Row(
                  children: [
                    Text(data.price.toString(),
                        style: TextStyle(fontSize: 20.0, color: Colors.red)),
                    Text('元/月(押一付三)',
                        style: TextStyle(fontSize: 14.0, color: Colors.red)),
                  ],
                ),
              ),
              Padding(
                padding: EdgeInsets.only(top: 4.0),
              ),
              Container(
                padding: EdgeInsets.only(left: 10.0),
                child: Wrap(
                  //mainAxisAlignment: MainAxisAlignment.center,
                  spacing: 4.0,
                  children: data.tags.map((e) => CommonTag(e)).toList(),
                ),
              ),
              Divider(
                //左右缩进
                indent: 10.0,
                endIndent: 10.0,
                color: Colors.grey,
              ),
              Container(
                child: Wrap(
                  children: [
                    CommonDetailMain(
                      titleOne: '面积',
                      titleTwo: '${data.size}平米',
                    ),
                    CommonDetailMain(
                      titleOne: '楼层',
                      titleTwo: data.floor,
                    ),
                    CommonDetailMain(
                      titleOne: '房型',
                      titleTwo: data.roomType,
                    ),
                    CommonDetailMain(
                      titleOne: '装修',
                      titleTwo: data.renovation,
                    ),
                  ],
                ),
              ),
              CommonTitle("房屋配置"),
              RoomApplicanceList(
                list: data.applicances,
              ),
              CommonTitle("房屋概况"),
              Container(
                //height: 200.0,
                padding: EdgeInsets.only(left: 10.0, right: 10.0),
                child: Column(children: [
                  Text(
                    data.subTitle ?? '暂无房屋概况',
                    //如果是true就是全部展开,如果是false 就默认展示5行
                    maxLines: showAliText ? null : 5,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      showTextTool
                          ? GestureDetector(
                              onTap: () {
                                setState(() {
                                  showAliText = !showAliText;
                                });
                              },
                              child: Row(
                                children: [
                                  Text(showAliText ? '收起' : '展开'),
                                  Icon(showAliText
                                      ? Icons.keyboard_arrow_up
                                      : Icons.keyboard_arrow_down)
                                ],
                              ),
                            )
                          : Container(),
                      GestureDetector(
                        onTap: () {
                          print('举报......');
                        },
                        child: Text('举报'),
                      )
                    ],
                  )
                ]),
              ),
              CommonTitle("猜你喜欢"),
              InfoPage(),
              Padding(
                padding: EdgeInsets.only(bottom: 100.0),
              ),
            ],
          ),
          //浮动效果,对齐到底部
          Positioned(
            width: MediaQuery.of(context).size.width,
            height: 100.0,
            bottom: 0,
            child: Container(
              color: Colors.grey[200],
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  Padding(
                    padding: EdgeInsets.only(left: 10.0),
                  ),
                  GestureDetector(
                    onTap: () {
                      setState(() {
                        isLike = !isLike;
                      });
                    },
                    behavior: HitTestBehavior.opaque,
                    child: Container(
                      height: 50.0,
                      width: 40.0,
                      margin: EdgeInsets.only(right: 10.0),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          isLike
                              ? Icon(
                                  Icons.star,
                                  color: Colors.green,
                                )
                              : Icon(Icons.star_border),
                          isLike
                              ? Text('已收藏', style: TextStyle(fontSize: 12.0))
                              : Text('收藏', style: TextStyle(fontSize: 12.0))
                        ],
                      ),
                    ),
                  ),

                  //自适应
                  Expanded(
                    child: GestureDetector(
                      onTap: () {
                        Navigator.of(context).pushNamed("login");
                      },
                      child: Container(
                        decoration: BoxDecoration(
                          //背景
                          color: Colors.blue,
                          //设置四周圆角 角度
                          borderRadius: BorderRadius.all(Radius.circular(10.0)),
                          //设置四周边框
                          border: Border.all(width: 1, color: Colors.green),
                        ),
                        alignment: Alignment.center,
                        height: 50.0,
                        width: 140.0,
                        margin: EdgeInsets.only(right: 10.0),
                        child: Text('联系房东',
                            style: TextStyle(
                                fontSize: 18.0,
                                color: Colors.white,
                                fontWeight: FontWeight.w500)),
                      ),
                    ),
                  ),
                  //自适应
                  Expanded(
                    child: GestureDetector(
                      onTap: () {
                        Navigator.of(context).pushNamed("login");
                      },
                      child: Container(
                        decoration: BoxDecoration(
                          //背景
                          color: Colors.green,
                          //设置四周圆角 角度
                          borderRadius: BorderRadius.all(Radius.circular(10.0)),
                          //设置四周边框
                          border: Border.all(width: 1, color: Colors.green),
                        ),
                        alignment: Alignment.center,
                        height: 50.0,
                        width: 140.0,
                        margin: EdgeInsets.only(right: 10.0),
                        child: Text('预约看房',
                            style: TextStyle(
                                fontSize: 18.0,
                                color: Colors.white,
                                fontWeight: FontWeight.w500)),
                      ),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.only(right: 2.0),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
